package excel

import (
	"fmt"
	"strconv"
	"strings"
)

func convertCell(cell string) (res []float64) {
	res = make([]float64, 0)
	cellLen := len(cell)

	if cellLen == 0 {
		return
	}

	num := cellLen / 5

	for i := 0; i < num; i++ {
		start := 5 * i
		end := start + 5
		tpStr := cell[start:end]
		tpArr := strings.Split(tpStr, ":")
		//fmt.Println(tmp1, reflect.TypeOf(tmp1))
		big, _ := strconv.Atoi(tpArr[0])
		little1, _ := strconv.Atoi(tpArr[1])
		little2 := float64(little1) / 60
		little3, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", little2), 64)
		tp := float64(big) + little3
		//fmt.Println(big, reflect.TypeOf(big), little3, reflect.TypeOf(little3), tp, reflect.TypeOf(tp))
		res = append(res, tp)
	}
	fmt.Println(res)
	return res
}

func getWorkHour(cell string) float64 {
	clockList := convertCell(cell)
	res := calculateCell(clockList)
	//hour, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", res), 64)
	//fmt.Println(hour)
	return res
}

var RulePart = [][]float64{
	{0, 6.5},
	{5.25, 11.75},
	{13.45, 19.75},
	{18, 24},
}

var WorkPart = [][]float64{
	{0, 5.5},
	{6.25,10.75},
	{14.25,18.75},
	{19,24},
}

func calculateCell(list []float64) float64 {
	//小于等于1个值，无法计算，为0
	if len(list) <= 1 {
		return 0
	}

	var (
		clockPart = make([][]float64, 4)
		listPart  = make([][]float64, 4)
		timePart = make([]float64, 0)
		total float64
	)

	if list[0] < 1 {
		clockPart[0], listPart[0] = cutTimePart(0, list)
	} else {
		clockPart[0], listPart[0] = []float64{0, 0}, list
	}
	clockPart[1], listPart[1] = cutTimePart(1, listPart[0])
	clockPart[2], listPart[2] = cutTimePart(2, listPart[1])
	if clockPart[2][1] > RulePart[3][0] {
		list2 := make([]float64, 0)
		list2 = append(list2, clockPart[2][1])
		list2 = append(list2, listPart[2]...)
		clockPart[3], listPart[3] = cutTimePart(3, list2)
	}


	for i, part := range clockPart {
		tmp := intersection(i, part)
		timePart = append(timePart, tmp)
		total += tmp
	}

	fmt.Println(clockPart)
	fmt.Println(listPart)
	fmt.Println(timePart)

	return total
}

//取交集
func intersection(part int, clockTime []float64) float64 {
	if len(clockTime) == 0 {
		return 0
	}
	if clockTime[0] < RulePart[part][0] || clockTime[1] > RulePart[part][1] {
		return 0
	}
	var big, little float64
	if WorkPart[part][0] < clockTime[0] {
		little = clockTime[0]
	} else {
		little = WorkPart[part][0]
	}

	if WorkPart[part][1] > clockTime[1] {
		big = clockTime[1]
	} else {
		big = WorkPart[part][1]
	}
	fmt.Println(big, little)
	res := big - little
	if res > 0 {
		return res
	}
	return 0
}

//传入规则和打卡时间列表，返回规则内的时间范围，和剩余的打卡时间段列表
func cutTimePart(part int, list []float64) (scopes, resList []float64) {

	var min, max float64
	tmpList := make([]float64, 0)
	for _, item := range list {
		if item >= RulePart[part][0] && item <= RulePart[part][1] {
			tmpList = append(tmpList, item)
		}
	}
	if len(tmpList) > 0 {
		if tmpList[0] <= WorkPart[part][1] {
			min = tmpList[0]
		}

		tmp := -1
		for i, item := range tmpList {
			if item >= WorkPart[part][1] {
				max = item
				tmp = i
				break
			}
		}
		if tmp == -1 {
			max = tmpList[len(tmpList) - 1]
			resList = list[len(tmpList):]
		} else {
			resList = list[tmp+1:]
		}
	} else {
		resList = list
	}

	return []float64{min, max}, resList
}

